import 'package:abc/controller/share_promotion_controller.dart';
import 'package:abc/page/invite_record_page.dart';
import 'package:abc/view/CustomAppBar.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:qr_flutter/qr_flutter.dart';

class SharePromotionPage extends GetView<SharePromotionController> {
  const SharePromotionPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
  if (!Get.isRegistered<SharePromotionController>()) {
      Get.lazyPut(() => SharePromotionController());
    }
    return Scaffold(
        backgroundColor: const Color(0xFF1B182B), //  全屏深色背景
        appBar: CustomAppBar(
          title: "分享推广",
          onBack: () {
            Get.back();
          },
          rightText: "邀请记录", // 右上角文字
          onRightTextTap: () {
            // 跳转到邀请记录页面
            Get.to(() => const InviteRecordPage());
          },
        ),
        body: Stack(
          children: [
            Positioned(
                child: RepaintBoundary(
                    key: controller.captureKey, // 绑定全局 key
                    child: Stack(children: [
                      Positioned(
                        right: 0,
                        bottom: 80,
                        left: 0,
                        child: Image.asset(
                          'assets/images/share_promotion_bg.png',
                        ),
                      ),
                      Positioned(
                        right: 0,
                        bottom: 60,
                        left: 0,
                        child: Image.asset(
                          'assets/images/share_promotion_bottom_bg.png',
                        ),
                      ),
                      _centerDataWidget()
                    ]))),
            _bottomOptionBar(context)
          ],
        ));
  }

  Widget _centerDataWidget() {
    return Positioned(
      bottom: 140,
      left: 0,
      right: 0,
      child: Container(
        child: Column(
          children: [
            Container(
              // 四角圆弧半径 10
              decoration: BoxDecoration(
                // 背景颜色 #F34139
                color: const Color(0xFFF34139),
                borderRadius: BorderRadius.circular(10),
              ),
              // 内间距：垂直方向 1.5，水平方向 10
              padding: const EdgeInsets.symmetric(
                vertical: 1.5,
                horizontal: 10,
              ),
              // 可以在这里添加子组件
              child: const Text(
                "邀请码",
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 13.0,
                  fontWeight: FontWeight.w400,
                ),
              ),
            ),
            const SizedBox(
              height: 10,
            ),
            Obx(() => Text(
                  controller.inviteCode.value,
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 15.0,
                    fontWeight: FontWeight.w400,
                  ),
                )),
            const SizedBox(
              height: 22,
            ),
            Container(
                width: 102,
                height: 102,
                color: Colors.white,
                child: Obx(() => QrImageView(
                      data: controller.inviteCode.value, // 二维码内容
                      version: QrVersions.auto,
                      size: 100.0, // 调整大小以适应内边距
                      gapless: false,
                      errorStateBuilder: (cxt, err) {
                        return const Center(
                          child: Text(
                            '二维码生成失败',
                            style: TextStyle(color: Colors.red),
                          ),
                        );
                      },
                    ))),
            const SizedBox(
              height: 59,
            ),
            const Text(
              "分享APP给好友，可获得不同奖励",
              style: TextStyle(
                color: Colors.white,
                fontSize: 13.0,
                fontWeight: FontWeight.w400,
              ),
            ),
            const SizedBox(
              height: 10,
            ),
            const Text(
              "官网地址：https://www.baidu.com/",
              style: TextStyle(
                color: Colors.white,
                fontSize: 13.0,
                fontWeight: FontWeight.w400,
              ),
            ),
          ],
        ),
      ),
    );
  }

  //底部工具栏
  Widget _bottomOptionBar(BuildContext context) {
    return Positioned(
        right: 0,
        bottom: 0,
        left: 0,
        child: Container(
          width: double.infinity,
          height: 100,
          decoration: const BoxDecoration(
            color: Color(0xFF0E0F26),
            borderRadius: BorderRadius.vertical(
              top: Radius.circular(20),
              // 底部不设置圆角（默认0）
            ),
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Column(
                children: [
                  const SizedBox(
                    height: 17,
                  ),
                  Image.asset(
                    'assets/images/share_promotion_copy.png',
                    width: 44,
                    height: 44,
                  ),
                  const SizedBox(
                    height: 5,
                  ),
                  const Text(
                    '复制链接',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 13.0,
                      fontWeight: FontWeight.w400,
                    ),
                  )
                ],
              ),
              InkWell(
                child: Column(
                  children: [
                    const SizedBox(
                      height: 17,
                    ),
                    Image.asset(
                      'assets/images/share_promotion_save.png',
                      width: 44,
                      height: 44,
                    ),
                    const SizedBox(
                      height: 5,
                    ),
                    const Text(
                      '保存图片',
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 13.0,
                        fontWeight: FontWeight.w400,
                      ),
                    )
                  ],
                ),
                onTap: () {
                  controller.captureAndSaveToGallery(context);
                },
              )
            ],
          ),
        ));
  }
}
